package com.ys.web.mobao.model.po;

import com.ys.utils.base.j2se.DateHandler;
import com.ys.utils.base.j2se.StringHandler;
import com.ys.utils.mobao.Mobo360Config;
import com.ys.utils.spring.config.SystemError;
import com.ys.utils.spring.properties.Config;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * 钱包充值bean
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class MobaoRechargeCreate extends PayBase{
    public String cardToken;
    public String amount;
    public String notifyUrl = Mobo360Config._notifyUrl + "/onlinePay/recharge/send";
    /** 支付订单id */
    public String paymentOrderId;
    /** 短信验证码 */
    public String code;
    /** 支付密码 */
    public String payPassword;
    /** 资金类型  1充值 2红包 3转账 4订单*/
    public int businessType = 1;
    /** 支付方式编码 	BANK_CARD-EXPRESS_DEBIT 快捷支付借记卡  ALIPAY-OFFICIAL-P2P 支付宝生活号支付 */
    /** 支付类型  1银行卡  2支付宝 */
    public int payCode = 1;
    /** 商品名称  充值不传 */
    public String goodsName;
    /** 商品订单 */
    public String orderId;

    public void check(String uid) {
        if(this.businessType==1){
            if (this.amount.contains(".")) {
                SystemError.wrapBs("400", "充值金额不能有小数");
            }
            if (StringHandler.getLong(this.amount) <= 0) {
                SystemError.wrapBs("400", "金额错误");
            }

            long compareMoney = StringHandler.getLong(StringHandler.parseMoney(amount));
            long min = StringHandler.getLong(Config.pp.get("wallet.recharge.money.min"));
            if (compareMoney < min) {
                SystemError.wrapBs("400", "充值金额不能小于" + StringHandler.moneyDesc(min) + "元");
            }
            long max = StringHandler.getLong(Config.pp.get("wallet.recharge.money.max"));
            if (compareMoney > max) {
                SystemError.wrapBs("400", "充值金额不能大于" + StringHandler.moneyDesc(max) + "元");
            }
            String hours_str = StringHandler.getString(Config.pp.get("wallet.recharge.hour.chenck.list"));
            if("zfb".equals(this.payChannel)){
                if (!Boolean.parseBoolean(Config.pp.get("wallet.recharge.hour.chenck.open")) || StringHandler.isEmpty(hours_str)) {
                    LocalDateTime time = LocalDateTime.now();
                    int hour1 = StringHandler.getInt(Config.pp.get("wallet.recharge.hour.min"));
                    int hour2 = StringHandler.getInt(Config.pp.get("wallet.recharge.hour.max"));
                    if (!this.whiteList(uid)) {
                        if (time.getHour() < hour1 || time.getHour() > hour2) {
                            SystemError.wrapBs("400", String.format("每日充值时间为%s点到%s点", hour1, hour2));
                        }
                    }
                }else{
                    long hours = DateHandler.getHour();
                    List<String> list = Arrays.asList(hours_str.split(","));
                    if(list.contains(StringHandler.getString(hours))){
                        SystemError.wrapBs("400", "请在"+hours_str+"点之外时间充值");
                    }
                }
            }
        }

        this.amount = String.valueOf(StringHandler.yuanToFen(this.amount));
    }
    public boolean whiteList(String uid){
        String str =Config.pp.get("wallet.user.white.list");
        if(StringHandler.isNotEmpty(str)){
            List<String> list = Arrays.asList(str.split(","));
            if(list.contains(uid)){
                return true;
            }
        }
        return false;
    }
    public String getCardToken() {
        return cardToken;
    }

    public void setCardToken(String cardToken) {
        this.cardToken = cardToken;
    }

    public String getAmount() {
        return amount;
    }

    public void setAmount(String amount) {
        this.amount = amount;
    }

    public String getNotifyUrl() {
        return notifyUrl;
    }

    public void setNotifyUrl(String notifyUrl) {
        this.notifyUrl = notifyUrl;
    }

    public String getPaymentOrderId() {
        return paymentOrderId;
    }

    public void setPaymentOrderId(String paymentOrderId) {
        this.paymentOrderId = paymentOrderId;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getPayPassword() {
        return payPassword;
    }

    public void setPayPassword(String payPassword) {
        this.payPassword = payPassword;
    }

    public int getBusinessType() {
        return businessType;
    }

    public void setBusinessType(int businessType) {
        this.businessType = businessType;
    }

    public int getPayCode() {
        return payCode;
    }

    public void setPayCode(int payCode) {
        this.payCode = payCode;
    }

    public String getGoodsName() {
        return goodsName;
    }

    public void setGoodsName(String goodsName) {
        this.goodsName = goodsName;
    }

    public String getOrderId() {
        return orderId;
    }

    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }
}
